package utils.msg;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import utils.PropertiesUtil;

/**
 * 短信发送工具类
 * @author zhrb
 *
 */
public class JavaMail {
	// 收件人邮箱地址
	private static String to;
	// 发件人邮箱地址
	private static String from = PropertiesUtil.getValue("sender");
	// SMTP服务器地址
	private static String smtpServer = PropertiesUtil.getValue("smtpServer");
	// 登录SMTP服务器的用户名
	private static String username = PropertiesUtil.getValue("mailuserName");
	// 登录SMTP服务器的密码
	private static String password = PropertiesUtil.getValue("mailPassword");
	// 邮件主题
	private static String subject = "智能预警";
	// 邮件正文
	private static String content;
	// 记录所有附件文件的集合
	static List<String> attachments = new ArrayList<String>();
	// log
	static Logger logger = LoggerFactory.getLogger(JavaMail.class.getName());

	// content属性的setter方法
	public void setContent(String content) {
		JavaMail.content = content;
	}

	// 把邮件主题转换为中文
	public static String transferChinese(String strText) {
		try {
			strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strText;
	}

	// 增加附件，将附件文件名添加的List集合中
	public static void attachfile(String fname) {
		attachments.add(fname);
	}

	public static boolean sendMail(String msg, String email) {
		to = email;
		content = msg;
		return send();
	}

	public static boolean sendMail(String msg, String email, String attachment) {
		to = email;
		content = msg;
		attachfile(PropertiesUtil.getValue("attachfile"));
		return sendwithattachfile();
	}

	// 发送邮件，不带附件
	public static boolean send() {
		// 创建邮件Session所需的Properties对象
		Properties props = new Properties();
		props.put("mail.smtp.host", smtpServer);
		props.put("mail.smtp.auth", "true");
		// 创建Session对象
		Session session = Session.getDefaultInstance(props
		// 以匿名内部类的形式创建登录服务器的认证对象
				, new Authenticator() {
					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});
		try {
			// 构造MimeMessage并设置相关属性值
			MimeMessage msg = new MimeMessage(session);
			// 设置发件人
			msg.setFrom(new InternetAddress(from));
			// 设置收件人
			InternetAddress[] addresses = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, addresses);
			// 设置邮件主题
			//subject = transferChinese(subject);
			// 构造Multipart
			Multipart mp = new MimeMultipart();
			msg.setSubject(subject);
			// 向Multipart添加正文
			MimeBodyPart mbpContent = new MimeBodyPart();
			mbpContent.setText(content);
			// 将BodyPart添加到MultiPart中
			mp.addBodyPart(mbpContent);
			// 向Multipart添加MimeMessage
			msg.setContent(mp);
			// 设置发送日期
			msg.setSentDate(new Date());
			// 发送邮件
			Transport.send(msg);
		} catch (MessagingException mex) {
			logger.debug("fail {}.", mex.toString());
			// mex.printStackTrace();
			return false;
		}
		return true;
	}

	// 发送带附件的邮件
	public static boolean sendwithattachfile() {
		// 创建邮件Session所需的Properties对象
		Properties props = new Properties();
		props.put("mail.smtp.host", smtpServer);
		props.put("mail.smtp.auth", "true");
		// 创建Session对象
		Session session = Session.getDefaultInstance(props
		// 以匿名内部类的形式创建登录服务器的认证对象
				, new Authenticator() {
					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				});
		try {
			// 构造MimeMessage并设置相关属性值
			MimeMessage msg = new MimeMessage(session);
			// 设置发件人
			msg.setFrom(new InternetAddress(from));
			// 设置收件人
			InternetAddress[] addresses = { new InternetAddress(to) };
			msg.setRecipients(Message.RecipientType.TO, addresses);
			// 设置邮件主题
			subject = transferChinese(subject);
			msg.setSubject(subject);
			// 构造Multipart
			Multipart mp = new MimeMultipart();
			// 向Multipart添加正文
			MimeBodyPart mbpContent = new MimeBodyPart();
			mbpContent.setText(content);
			// 将BodyPart添加到MultiPart中
			mp.addBodyPart(mbpContent);
			// 向Multipart添加附件
			// 遍历附件列表，将所有文件添加到邮件消息里
			for (String efile : attachments) {
				MimeBodyPart mbpFile = new MimeBodyPart();
				// 以文件名创建FileDataSource对象
				FileDataSource fds = new FileDataSource(efile);
				// 处理附件
				mbpFile.setDataHandler(new DataHandler(fds));
				mbpFile.setFileName(fds.getName());
				// 将BodyPart添加到MultiPart中
				mp.addBodyPart(mbpFile);
			}
			// 清空附件列表
			attachments.clear();
			// 向Multipart添加MimeMessage
			msg.setContent(mp);
			// 设置发送日期
			msg.setSentDate(new Date());
			// 发送邮件
			Transport.send(msg);
		} catch (MessagingException mex) {
			mex.printStackTrace();
			return false;
		}
		return true;
	}
}
